<template>
  <div class="category">
    <div class="category-title">
      <h2>分类</h2>
    </div>
    <div class="category-content">
      <div class="category-left">
        <el-tabs tab-position="left" type="card" v-model="activeName" @tab-click="handleClick">
          <el-tab-pane v-for="(item, index) in categoryList" :key="index" :label="item.name" :name="item.id + ''">
          </el-tab-pane>
        </el-tabs>
      </div>
      <div class="category-right">
        <div v-if="!articleList || articleList.length === 0">
          <h4>暂无数据</h4>
        </div>
        <div v-else>
          <h3>共{{ total }}篇文章</h3>
        </div>
        <div class="article-content">
          <el-row id="artList" type="flex" justify="space-around" v-for="item of articleList" :key="item.id">
            <el-col>
              <el-row class="art-item">
                <el-card shadow="hover">
                  <h5>
                    <router-link :to="'/article/' + item.id" tag="span" class="art-title">{{ item.title }}</router-link>
                  </h5>
                  <el-row class="art-info d-flex align-items-center justify-content-start">
                    <div class="art-time">
                      <i class="el-icon-time"/>：{{ item.createTime }}
                    </div>
                    <div class="d-flex align-items-center">
                      <i class="iconfont icon-24gf-tags2"/>：
                      <span class="art-tag" v-for="tagItem of item.blogTagList" :key="tagItem.id"><el-tag size="mini">{{
                          tagItem.name
                        }}</el-tag></span>
                    </div>
                  </el-row>
                  <el-row class="art-body">
                    <div class="side-img hidden-sm-and-down">
                      <router-link :to="'/article/' + item.id"><img class="art-banner" :src="item.avatar"></router-link>
                    </div>
                    <div class="side-abstract">
                      <div class="art-abstract">
                        {{ replaceHtml(item.content) }}
                      </div>
                      <div class="art-more">
                        <router-link :to="'/article/' + item.id" tag="span">
                          <el-button plain>阅读全文</el-button>
                        </router-link>
                        <div class="view"><i class="el-icon-view"/>{{ item.quantity }}</div>
                      </div>
                    </div>
                  </el-row>
                  <i class="iconfont icon-top01 star"></i>
                </el-card>
              </el-row>
            </el-col>
          </el-row>
        </div>
        <div class="block pagination">
          <el-pagination background layout="prev, pager, next" :current-page="currentPage" :page-size="pageSize"
                         :total="total" @current-change="handleCurrentChange"></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {listCategory, listArticle} from "@/api";
import {replaceHtml} from "@/views/article";

export default {
  name: "category",
  data() {
    return {
      categoryList: [],
      activeName: '0',
      articleList: [],
      pageSize: 5,
      currentPage: 1,
      total: 0
    };
  },
  components: {},
  mounted() {
    this.listCategory();
    this.listArticle();
  },
  methods: {
    listCategory() {
      listCategory().then(res => {
        this.categoryList = res.data;
        this.activeName = this.categoryList[0].id + '';
      });
    },
    listArticle() {
      listArticle({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        categoryId: Number.parseInt(this.activeName)
      }).then(res => {
        this.articleList = res.data.results;
        this.currentPage = res.data.pageNum;
        this.total = res.data.total;
      });
    },
    handleClick(tab, event) {
      console.log(tab, event);
      this.listArticle();
    },
    handleCurrentChange: function (currentPage) {
      this.currentPage = currentPage;
      this.listArticle();
    },
    // html只保留文本
    replaceHtml(val) {
      return replaceHtml(val);
    }
  }
};
</script>

<style scoped>
.category {
  margin: 20px 0;
  height: auto;
  min-height: 830px;
  background-color: #fff;
}

.category-title {
  height: 70px;
  line-height: 70px;
  margin-left: 20px;
}

.category-content {
  display: flex;
  margin: 0 20px 20px 20px;
}

.category-left {

}

.category-right {
  margin: 0 20px 20px 20px;
}

.article-content {
  min-height: 690px;
}

.art-item {
  margin-bottom: 20px;
  position: relative;
}

.art-title {
  border-left: 3px solid #F56C6C;
  padding-left: 5px;
  cursor: pointer;
}

.art-title:hover {
  padding-left: 10px;
  color: #409EFF;
}

.art-time {
  margin-right: 20px;
}

.art-body {
  display: flex;
  padding: 10px 0;
}

.side-img {
  height: 150px;
  width: 270px;
  overflow: hidden;
  margin-right: 10px;
}

.art-item .star {
  font-size: 20px;
  width: 60px;
  height: 60px;
  position: absolute;
  color: orangered;
  top: 2%;
  right: 0;
}

img.art-banner {
  width: 100%;
  height: 100%;
  transition: all 0.6s;
}

img.art-banner:hover {
  transform: scale(1.4);
}

.side-abstract {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.art-abstract {
  flex: 1;
  color: #aaa;
}

.art-more {
  height: 40px;
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
}

.art-more .view {
  color: #aaa;
}

.art-info {
  margin-top: 20px;
  display: flex;
}

.art-tag {
  margin-left: 5px;
}

h5 {
  font-size: 18px;
}

</style>
